package com.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.fly.req.FlyDelReq;
import com.fire.bo.fly.req.FlyListReq;
import com.fire.bo.fly.req.FlyReq;
import com.fire.bo.fly.res.FlyRes;
import com.fire.common.RspObjBean;
import com.fire.service.FlyService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/fly")
@Api(value = "飞窗", description = "飞窗")
public class FlyController
{
	@Autowired
	private FlyService flyService;
	
	@ApiOperation(value = "飞窗新增", notes = "飞窗新增")
    @RequestMapping(value = "/insertFly", method = RequestMethod.POST)
    public @ResponseBody RspObjBean insertFly(@RequestBody @Valid FlyReq beans, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	FlyReq bean = flyService.insertFly(beans);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, bean);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
	@ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delFly", method = RequestMethod.POST)
    public @ResponseBody RspObjBean delFly(@RequestBody @Valid FlyDelReq beans, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	flyService.delFly(beans);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
	@ApiOperation(value = "飞窗列表查询", notes = "飞窗列表查询", response = FlyRes.class)
    @RequestMapping(value = "/findFlyList", method = RequestMethod.POST)
    public @ResponseBody RspObjBean findFlyList(@RequestBody @Valid FlyListReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	List<FlyRes> list = flyService.findFlyList(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, list,flyService.findFlyListCount(bean));
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
}
